package org.jeecg.modules.flower.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.flower.entity.FlowerPlantingGreenhouse2;
import org.jeecg.modules.flower.entity.FlowerPlantingBase2;
import org.jeecg.modules.flower.vo.FlowerPlantingBase2Page;
import org.jeecg.modules.flower.service.IFlowerPlantingBase2Service;
import org.jeecg.modules.flower.service.IFlowerPlantingGreenhouse2Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 基地管理
 * @Author: jeecg-boot
 * @Date:   2022-12-02
 * @Version: V1.0
 */
@Api(tags="基地管理")
@RestController
@RequestMapping("/flower/flowerPlantingBase2")
@Slf4j
public class FlowerPlantingBase2Controller {
	@Autowired
	private IFlowerPlantingBase2Service flowerPlantingBase2Service;
	@Autowired
	private IFlowerPlantingGreenhouse2Service flowerPlantingGreenhouse2Service;
	
	/**
	 * 分页列表查询
	 *
	 * @param flowerPlantingBase2
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "基地管理-分页列表查询")
	@ApiOperation(value="基地管理-分页列表查询", notes="基地管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FlowerPlantingBase2 flowerPlantingBase2,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FlowerPlantingBase2> queryWrapper = QueryGenerator.initQueryWrapper(flowerPlantingBase2, req.getParameterMap());
		Page<FlowerPlantingBase2> page = new Page<FlowerPlantingBase2>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
		queryWrapper.inSql("id","select base_id from flower_storehouse a,flower_storehouse_detail b where a.id = b.mainid and  b.personcode = '"+sysUser.getUsername()+"'");
		IPage<FlowerPlantingBase2> pageList = flowerPlantingBase2Service.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param flowerPlantingBase2Page
	 * @return
	 */
	@AutoLog(value = "基地管理-添加")
	@ApiOperation(value="基地管理-添加", notes="基地管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FlowerPlantingBase2Page flowerPlantingBase2Page) {
		FlowerPlantingBase2 flowerPlantingBase2 = new FlowerPlantingBase2();
		BeanUtils.copyProperties(flowerPlantingBase2Page, flowerPlantingBase2);
		flowerPlantingBase2Service.saveMain(flowerPlantingBase2, flowerPlantingBase2Page.getFlowerPlantingGreenhouse2List());
		return Result.OK("添加成功!");
	}
	
	/**
	 * 编辑
	 *
	 * @param flowerPlantingBase2Page
	 * @return
	 */
	@AutoLog(value = "基地管理-编辑")
	@ApiOperation(value="基地管理-编辑", notes="基地管理-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody FlowerPlantingBase2Page flowerPlantingBase2Page) {
		FlowerPlantingBase2 flowerPlantingBase2 = new FlowerPlantingBase2();
		BeanUtils.copyProperties(flowerPlantingBase2Page, flowerPlantingBase2);
		flowerPlantingBase2Service.updateMain(flowerPlantingBase2, flowerPlantingBase2Page.getFlowerPlantingGreenhouse2List());
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "基地管理-通过id删除")
	@ApiOperation(value="基地管理-通过id删除", notes="基地管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
	    flowerPlantingBase2Service.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "基地管理-批量删除")
	@ApiOperation(value="基地管理-批量删除", notes="基地管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.flowerPlantingBase2Service.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
     *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "基地管理-通过id查询")
	@ApiOperation(value="基地管理-通过id查询", notes="基地管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FlowerPlantingBase2 flowerPlantingBase2 = flowerPlantingBase2Service.getById(id);
		return Result.OK(flowerPlantingBase2);
	}
	
	/**
	 * 通过id查询
     *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "基地大棚管理-通过主表ID查询")
	@ApiOperation(value="基地大棚管理-通过主表ID查询", notes="基地管理-通过主表ID查询")
	@GetMapping(value = "/queryFlowerPlantingGreenhouse2ByMainId")
	public Result<?> queryFlowerPlantingGreenhouse2ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FlowerPlantingGreenhouse2> flowerPlantingGreenhouse2List = flowerPlantingGreenhouse2Service.selectByMainId(id);
		return Result.OK(flowerPlantingGreenhouse2List);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param flowerPlantingBase2
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, FlowerPlantingBase2 flowerPlantingBase2) {
      // Step.1 组装查询条件
      QueryWrapper<FlowerPlantingBase2> queryWrapper = QueryGenerator.initQueryWrapper(flowerPlantingBase2, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<FlowerPlantingBase2Page> pageList = new ArrayList<FlowerPlantingBase2Page>();
      List<FlowerPlantingBase2> flowerPlantingBase2List = flowerPlantingBase2Service.list(queryWrapper);
      for (FlowerPlantingBase2 temp : flowerPlantingBase2List) {
          FlowerPlantingBase2Page vo = new FlowerPlantingBase2Page();
          BeanUtils.copyProperties(temp, vo);
          List<FlowerPlantingGreenhouse2> flowerPlantingGreenhouse2List = flowerPlantingGreenhouse2Service.selectByMainId(temp.getId());
          vo.setFlowerPlantingGreenhouse2List(flowerPlantingGreenhouse2List);
          pageList.add(vo);
      }
      //Step.3 调用AutoPoi导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "基地管理");
      mv.addObject(NormalExcelConstants.CLASS, FlowerPlantingBase2Page.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("基地管理数据", "导出人:"+sysUser.getRealname(), "基地管理"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<FlowerPlantingBase2Page> list = ExcelImportUtil.importExcel(file.getInputStream(), FlowerPlantingBase2Page.class, params);
              for (FlowerPlantingBase2Page page : list) {
                  FlowerPlantingBase2 po = new FlowerPlantingBase2();
                  BeanUtils.copyProperties(page, po);
                  flowerPlantingBase2Service.saveMain(po, page.getFlowerPlantingGreenhouse2List());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
  }

}
